/*
 * FileCopier.java
 *
 * Created on October 20, 2007, 4:46 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package filecopier;

import javax.swing.JFrame;
import javax.swing.JDialog;
import filecopier.options.Options;
import jargs.gnu.CmdLineParser;
import javax.swing.UIManager;

import org.apache.log4j.Logger;

/**
 *
 * @author david
 */
public class FileCopier {

    /** Creates a new instance of FileCopier */
    public FileCopier(String[] args) {
        Singleton singleton = null;
        try {
            singleton = initSingleton(args);
            Options.UIMode uiMode = singleton.getOptions().getUIMode();
            printArgs(args);

            if (uiMode == Options.UIMode.Gui) {
                final Options options = singleton.getOptions();
                if(options.useWindowDecorations()) {
                    JFrame.setDefaultLookAndFeelDecorated(true);
                    JDialog.setDefaultLookAndFeelDecorated(true);
                    java.awt.Toolkit.getDefaultToolkit().setDynamicLayout(true);
                    System.setProperty("sun.awt.noerasebackground", "true");
                } else {
                    JFrame.setDefaultLookAndFeelDecorated(false);
                    JDialog.setDefaultLookAndFeelDecorated(false);
                }
                javax.swing.SwingUtilities.invokeLater(new Runnable() {

                    public void run() {
                        try {
                            UIManager.setLookAndFeel(options.getLookAndFeel().getClassName());
                            if(options.useWindowDecorations()
                                && !UIManager.getLookAndFeel().getSupportsWindowDecorations()) {
                                Singleton.getInstance().getFileConsoleLogger().warn("FileCopier tried to use Swing "
                                    + "window decorations, but the current look & feel, "
                                    + UIManager.getLookAndFeel().getName() + ", does not provide its own window "
                                    + "decorations. Native window decorations will be used.");
                            }
                        } catch(Exception e) {
                            Singleton.getInstance().getFileConsoleLogger().error("Attempt to load look & feel \""
                                + options.getLookAndFeel().getClassName() + "\" failed.");
                        }
                        new filecopier.gui.FileCopier("FileCopier");
                    }
                });
            } else {
                new filecopier.console.FileCopier();
                singleton.saveLog4jFile();
            }

        } catch (FileCopierException e) {
            // The file console logger logs to the log file and to the console.
            Logger logger = null;
            if (singleton != null && (logger = singleton.getFileConsoleLogger()) != null) {
                logger.fatal("FileCopierException encountered", e);
            } else {
                e.printStackTrace();
            }
        } catch (CmdLineParser.OptionException e) {
            Logger logger = null;
            if (singleton != null && (logger = singleton.getFileConsoleLogger()) != null) {
                logger.fatal("CmdLineParser.OptionException encountered", e);
            } else {
                e.printStackTrace();
            }
        } catch (Exception e) {
            Logger logger = null;
            if (singleton != null && (logger = singleton.getFileConsoleLogger()) != null) {
                logger.fatal("Unknown Exception encountered", e);
            } else {
                e.printStackTrace();
            }
        }
    }

    private Singleton initSingleton(String[] args) throws FileCopierException, CmdLineParser.OptionException {
        Singleton singleton = Singleton.getInstance();
        singleton.init(args);
        return singleton;
    }

    private void printArgs(String[] args) {
        // The main logger logs only to the log file.
        Logger logger = Singleton.getInstance().getFileAlwaysLogger();

        if (args.length > 0) {
            StringBuffer sb = new StringBuffer();
            sb.append("FileCopier starting with the following arguments: ");
            for (String s : args) {
                sb.append(" " + s);
            }
            logger.info(sb.toString());
        } else {
            logger.info("FileCopier starting with no arguments.");
        }
    }

    public static void main(String[] args) {
		// Check for Java 1.6 or later
		String javaVersion = System.getProperty("java.version");
		if(javaVersion.compareTo("1.6") < 0) {
			System.err.println("You are running Java version "
				+ javaVersion + '.');
			System.err.println("FileCopier requires Java 1.6 or later.");
			System.exit(1);
		}        
        new FileCopier(args);
    }
}
